package com.atguigu.serviceedu.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.serviceedu.clients.MemberClient;
import com.atguigu.serviceedu.clients.vo.LoginInfoVo;
import com.atguigu.serviceedu.entity.EduComment;
import com.atguigu.serviceedu.service.EduCommentService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-09-15
 */
@RestController
@RequestMapping("/eduservice/comment")
public class EduCommentController {
    @Autowired
    private EduCommentService commentService;

    @Autowired
    private MemberClient memberClient;

    /**
     *  根据课程id得到所有评论
     * @return
     */
    @PostMapping("{page}/{limit}")
    public R index(@PathVariable long page, @PathVariable long limit, @Param("courseId") String courseId){
        Page<EduComment> commentPage=new Page<>(page,limit);
        Map<String,Object> map=commentService.getComment(commentPage,courseId);
        return R.ok().data(map);
    }

    @PostMapping("addcomment/auth")
    public R save(@RequestBody EduComment comment, HttpServletRequest request){
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        if(StringUtils.isEmpty(memberId)){
            return R.error().code(28004).message("请先登陆再评论！");
        }
        comment.setMemberId(memberId);
        LoginInfoVo info = memberClient.getInfo(memberId);
        comment.setNickname(info.getNickname());
        comment.setAvatar(info.getAvatar());
        commentService.save(comment);
        return R.ok();
    }

}

